﻿using System.Runtime.Serialization;
using System.Windows;
using GA.Shared.Interfaces;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Windows.Data;
using GA.Server.DatabaseManager;
using GA.Shared.Objects;
using System;

namespace GA.Server.Objects
{
    [Serializable]
    public class DatabaseUser : DatabaseObject
    {
        /// <summary>
        /// Defines the Name Property.
        /// </summary>
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register(
                "Name",
                typeof(string),
                typeof(DatabaseUser),
                new PropertyMetadata(string.Empty));

        /// <summary>
        /// Defines the IsOnline Property.
        /// </summary>
        public static readonly DependencyProperty IsOnlineProperty =
            DependencyProperty.Register(
                "IsOnline",
                typeof(bool),
                typeof(DatabaseUser),
                new PropertyMetadata(false));

        /// <summary>
        /// Defines the Id Property.
        /// </summary>
        public static readonly DependencyProperty IdProperty =
            DependencyProperty.Register(
                "Id",
                typeof(int),
                typeof(DatabaseUser),
                new PropertyMetadata(-1));

        /// <summary>
        /// Defines the Authentication Key Property.
        /// </summary>
        public static readonly DependencyProperty AuthenticationKeyProperty =
            DependencyProperty.Register(
                "AuthenticationKey",
                typeof(byte[]),
                typeof(DatabaseUser),
                new PropertyMetadata(null));

        /// <summary>
        /// Defines the AgencyId Property.
        /// </summary>
        public static readonly DependencyProperty AgencyIdProperty =
            DependencyProperty.Register(
                "AgencyId",
                typeof(int?),
                typeof(DatabaseUser),
                new PropertyMetadata(null));

        /// <summary>
        /// Defines the Agency Property.
        /// </summary>
        public static readonly DependencyProperty AgencyProperty =
            DependencyProperty.Register(
                "Agency",
                typeof(Agency),
                typeof(DatabaseUser),
                new PropertyMetadata(null));

        /// <summary>
        /// Defines the Permissions Property.
        /// </summary>
        public static readonly DependencyProperty PermissionsProperty =
            DependencyProperty.Register(
                "Permissions",
                typeof(ObservableCollection<Permission>),
                typeof(DatabaseUser),
                new PropertyMetadata(null));

        /// <summary>
        /// Defines the Players Property.
        /// </summary>
        public static readonly DependencyProperty PlayersProperty =
            DependencyProperty.Register(
                "Players",
                typeof(ObservableCollection<Player>),
                typeof(DatabaseUser),
                new PropertyMetadata(null));

        /// <summary>
        /// Gets or sets the name of this user.
        /// </summary>
        
        [DatabaseField("Name", IsKey = true, IsQuoted = true)]
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        /// <summary>
        /// Gets or sets if this user is online.
        /// </summary>
        
        public bool IsOnline
        {
            get { return (bool)GetValue(IsOnlineProperty); }
            set { SetValue(IsOnlineProperty, value); }
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        
        [DatabaseField("Id", IsKey = true, IsIdentity = true)]
        public int Id
        {
            get { return (int)GetValue(IdProperty); }
            set { SetValue(IdProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Agency id.
        /// </summary>
        
        [DatabaseField("AgencyId")]
        public int? AgencyId
        {
            get { return (int?)GetValue(AgencyIdProperty); }
            set { SetValue(AgencyIdProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Agency.
        /// </summary>
        public Agency Agency
        {
            get { return (Agency)GetValue(AgencyProperty); }
            set { SetValue(AgencyProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Authentication Key.
        /// </summary>
        public byte[] AuthenticationKey
        {
            get { return (byte[])GetValue(AuthenticationKeyProperty); }
            set { SetValue(AuthenticationKeyProperty, value); }
        }

        /// <summary>
        /// Gets or sets the Permissions.
        /// </summary>
        
        public ObservableCollection<Permission> Permissions
        {
            get { return (ObservableCollection<Permission>)SafeGetValue(PermissionsProperty); }
            set { SafeSetValue(PermissionsProperty, value); }
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public DatabaseUser()
        {
        }

        /// <summary>
        /// Constructor for copying the baseUser.
        /// </summary>
        /// <param name="baseUser">Base user to connect to.</param>
        public DatabaseUser(LinkedDependencyObject baseUser)
            : base(baseUser)
        {
        }

        public DatabaseUser(LinkedDependencyObject baseUser, BindingMode bindingMode)
            : base(baseUser, bindingMode)
        {
        }

        /// <summary>
        /// Gets the Linked Properties.
        /// </summary>
        protected override DependencyProperty[] LinkedProperties
        {
            get { return mLinkedProperties; }
        }

        private DependencyProperty[] mLinkedProperties = new DependencyProperty[] { NameProperty, IsOnlineProperty, AgencyProperty, AgencyIdProperty };


        public override void LoadAllPropertyObjects()
        {
            #warning Not Implemented
        }

        public override void LoadPropertyObject(string propertyName)
        {
            #warning Not Implemented
        }
    }
}
